<template>
  <div class="login">
    <div class="login-background"></div>
    <div class="logo">
      <img src="../assets/logo.png" alt="">
      <div>
        统一身份认证
      </div>
    </div>
    <div class="form">
      <form>
        <h3>欢迎登录融合门户</h3>
        <div class="subtext">WELCOME!</div>
        <div class="form-item">
          <el-input placeholder="请输入账户" v-model="phone">
            <i slot="prefix" class="el-input__icon el-icon-user-solid"></i>
          </el-input>
        </div>
        <div class="form-item">
          <el-input show-password placeholder="请输入密码" v-model="password">
            <i slot="prefix" class="el-input__icon el-icon-lock"></i>
          </el-input>
        </div>
        <div class="form-item" style="text-align: left;">
          <el-checkbox>十天内免登录</el-checkbox>
        </div>
        <el-button @click="toLogin" class="login-btn" type="primary">立即登录</el-button>
         <el-button @click="casLogin" class="login-btn" type="primary" >cas登录</el-button>
      </form>
    </div>
  </div>
</template>

<script>
import { login } from "@/api/user";

export default {
  name: 'LoginView',
  components: {
  },
  data() {
    return {
      // phone: "15577262650",
      // password: "lzzy803273"
      // phone: "19167140637",
      // password: "888888"
      phone: "13217721025",
      password: "xcvisy@163.com"
      // phone: "",
      // password:""
    }
  },
  beforeCreate() {
    // location.href="http://authserver.lzzy.net/authserver/login?service=http://portal.lzpu.edu.cn/"
  },
  created() {
    // this.handleUrlParams();
  },

  mounted(){
    // location.href = "/teacher"
    //location.href="http://authserver.lzzy.net/authserver/login?service=http://portal.lzpu.edu.cn/"
  },

  methods: {
    // handleUrlParams() {
    //   // 从URL中获取ticket参数
    //   const params = new URLSearchParams(window.location.search);
    //   const ticket = params.get('ticket'); // 假设URL参数名为'ticket'
    //   if (ticket) {
    //     localStorage.setItem('ticket', ticket);
    //     //发送给后端做验证
    //     sendTicket(ticket).then(res => {
    //       console.log(res)
    //       if (res.code == 200) {
    //         this.$router.push("/teacher")
    //       }
    //     })
    //   } else {
    //     const storedTicket = localStorage.getItem('ticket');
    //     if (!storedTicket) {
    //       console.log("本地没有")
    //       this.startThirdPartyLogin();
    //     } else {
    //       console.log("本地存储有了")
    //       this.$router.push("/teacher")
    //     }
    //   }
    // },
    startThirdPartyLogin() {
      window.location.href = 'http://authserver.lzzy.net/authserver/login?service=http://portal.lzpu.edu.cn/';
    },
    casLogin(){
      window.location.href = 'http://authserver.lzzy.net/authserver/login?service=http://portal.lzpu.edu.cn/';
    },
    toLogin() {
      const formData = {
        "password": this.password,
        "phone": this.phone
      }

      // login(formData).then(res => {
      //   if (res.code == 200) {
      //     console.log('登录成功！')
      //     localStorage.setItem('authToken', res.data.token);
      //     this.$router.push("/teacher")
      //     this.$store.dispatch('fetchUser') // 调用action更新角色
      //   }
      // })
      this.$store.dispatch('doLogin',formData).then( ()=>{
        console.log('登录成功！')
        this.$router.push("/")
      }).catch(err=>{
        alert(err)
      })
    }
  }

}
</script>

<style lang="scss" scoped>
.login {
  position: relative;
  width: 100%;
  height: 100vh;
  overflow: hidden;

  /* 防止模糊效果溢出容器 */
  .login-background {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: url("../assets/logo-bg.jpg");
    background-size: cover;
    filter: blur(4px);
    z-index: -1;
    /* 确保背景在内容下方 */
  }

  .logo {
    padding: 10px 40px;
    display: flex;
    align-items: center;

    @include respond-to('phone') {
      padding: 10px 15px;

      img {
        height: 80px;
      }
    }

    &>div {
      color: white;
      margin-left: 10px;
      border-left: 1px solid white;
      padding: 14px 0;
      padding-left: 15px;

      @include respond-to('phone') {
        font-size: 13px;
      }
    }
  }

  .form {
    display: flex;
    justify-content: center;

    h3 {
      font-size: 28px;
      margin-bottom: 10px;
    }

    form {
      display: flex;
      flex-direction: column;
      text-align: center;
      margin-top: 50px;
      background-color: white;
      width: 400px;
      padding: 50px 25px;
      border-radius: 10px;

      @include respond-to('phone') {
        width: 80%;
        padding: 20px 25px;
      }

      .form-item {
        margin-top: 30px;

        .el-input {
          height: 45px;
          line-height: 45px;

          :deep input {
            border-radius: 10px;
            height: 45px;
          }

          ::placeholder {
            color: #C0C4CC;
          }

          i {
            // color: #4558E5;
            // font-weight: bold;
            font-size: 16px
          }
        }
      }

      .login-btn {
        background-color: #409EFF;
        margin-top: 30px;
        height: 50px;
      }

    }
  }
}
</style>
